package com.naicheng.ivote.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.naicheng.ivote.R;
import com.naicheng.ivote.constant.ConstantMine;
import com.naicheng.ivote.doman.BaseResponseEntity;
import com.naicheng.ivote.doman.MyMessageInfo;
import com.naicheng.ivote.engin.BaseActivity;
import com.naicheng.ivote.parser.BaseParser;
import com.naicheng.ivote.volley.Response.Listener;

public class MyMessageActivity extends BaseActivity {

	ListView lv_mineask;
	TextView main_title1;
	List<MyMessageInfo> list = new ArrayList<MyMessageInfo>();
	mAdapter adapter;
	TextView tv_back;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mineask);
		main_title1 = (TextView) this.findViewById(R.id.main_title1);
		main_title1.setText("我的消息");
		lv_mineask = (ListView) this.findViewById(R.id.lv_mineask);
		tv_back = (TextView) this.findViewById(R.id.tv_back);

		tv_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		for (int i = 0; i < 20; i++) {
			if (i % 2 == 0) {
				list.add(new MyMessageInfo("yiersan", "m", "20121012", 1, 1,
						"123456", "1234567890"));
			} else {
				list.add(new MyMessageInfo("yiersan", "f", "20121012", 1, 1,
						"123456", "1234567890"));
			}

		}
		adapter = new mAdapter();
		lv_mineask.setAdapter(adapter);
		// getData();
	}

	/**
	 * 初始化请求
	 */
	private void getData() {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("userid", ConstantMine.userid);
		super.getDataFromServer("myreceived", map, new Listener<JSONObject>() {

			@Override
			public void onResponse(JSONObject response) {
				System.out.println("response------>" + response.toString());
				BaseResponseEntity<MyMessageInfo> mResponse = new BaseParser<MyMessageInfo>()
						.parserCollection(response, MyMessageInfo.class);
				if (mResponse != null && mResponse.getState()) {
					list = mResponse.getData();
					System.out.println("list.size==========" + list.size());
					adapter = new mAdapter();
					lv_mineask.setAdapter(adapter);
				}

			}
		}, this);
	}

	class mAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(final int arg0, View view, ViewGroup arg2) {
			ItemView item = null;
			if (view == null) {
				item = new ItemView();
				view = View.inflate(getApplicationContext(),
						R.layout.item_mymessage, null);
				item.tv_title = (TextView) view.findViewById(R.id.tv_title);
				item.tv_content = (TextView) view.findViewById(R.id.tv_content);
				item.tv_time = (TextView) view.findViewById(R.id.tv_time);
				item.tv_username = (TextView) view
						.findViewById(R.id.tv_username);
				item.tv_usersex = (TextView) view.findViewById(R.id.tv_usersex);

				view.setTag(item);
			} else {
				item = (ItemView) view.getTag();
			}

			MyMessageInfo msg = list.get(arg0);

			if (msg != null) {
				if ("m".equals(msg.sex)) {
					item.tv_usersex.setText(getApplication().getResources()
							.getString(R.string.list_item_sex1));
					item.tv_usersex.setTextColor(getResources().getColor(
							R.color.sex_lan));
				} else {
					item.tv_usersex.setText(getApplication().getResources()
							.getString(R.string.list_item_sex2));
					item.tv_usersex.setTextColor(getResources().getColor(
							R.color.sex_zi));
				}
				item.tv_username.setText(msg.account);
				item.tv_time.setText(msg.thetime);
				item.tv_content.setText(msg.content);
				item.tv_title.setText(msg.title);

			}

			return view;
		}

	}

	class ItemView {
		public TextView tv_content;
		public TextView tv_title;
		public TextView tv_time;
		public TextView tv_username;
		public TextView tv_usersex;
	}

	/**
	 * removequestionnaire questionnaireid
	 * 
	 * @param position
	 */
	// private void delete(final int position){
	// MyquestionnaireRes res = list.get(position);
	// if(res!=null){
	// HashMap<String, String> map = new HashMap<String, String>();
	// map.put("userid", ConstantMine.userid);
	// map.put("questionnaireid", String.valueOf(res.id));
	//
	// super.getDataFromServer("removequestionnaire", map, new
	// Listener<JSONObject>() {
	//
	// @Override
	// public void onResponse(JSONObject response) {
	// System.out.println("response------>"
	// + response.toString());
	// BaseResponseEntity<String> mResponse = new
	// BaseParser<String>().parserString(response);
	// if(mResponse!=null&&mResponse.getState()){//删除成功，更新adapter
	//
	// list.remove(position);
	// adapter.notifyDataSetChanged();
	// }
	// }
	// }, this);
	// }
	// }

}
